package com.zb.factory.controller;


import cn.hutool.core.lang.Assert;
import com.zb.entity.AccounLoginInfo;
import com.zb.entity.SelectVo;
import com.zb.factory.service.ConFactoryService;
import com.zb.order.service.ConWorkSpareService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.service.ZjAccountBalanceAdjustService;
import com.zb.service.ZjFactoryEmpService;
import com.zb.service.ZjFactoryRechargeService;
import com.zb.service.ZjFactoryService;
import com.zb.utils.TokenUtil;
import com.zb.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.regex.Pattern;

/**
 * <p>
 * 厂商表 前端控制器
 * </p>
 *
 * @author WEN
 * @since 2024-02-27
 */
@Api(tags = "品牌商家类接口")
@RestController
@RequestMapping("/factory")
public class ZjFactoryController {
    @Resource
    private ConFactoryService conFactoryService;
    @Resource
    private ZjFactoryService factoryService;
    @Resource
    private ZjAccountBalanceAdjustService accountBalanceAdjustService;
    @Resource
    private ZjFactoryEmpService factoryEmpService;
    @Resource
    private ZjFactoryRechargeService factoryRechargeService;
    @Resource
    private ConWorkSpareService conWorkSpareService;

    @ApiOperation("新增或修改品牌商家")
    @PostMapping("/save")
    public Result save(HttpServletRequest request, @RequestBody @Valid FactoryVo factoryVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        factoryService.save(custAccId, factoryVo);
        return Result.success(null == factoryVo.getId()? "新增品牌商家成功" : "修改品牌商家成功");
    }

    @ApiOperation("获取品牌商家列表")
    @PostMapping("/getList")
    public Result getList(HttpServletRequest request, @RequestBody FactoryListSelectVo selectVo) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取品牌商家列表成功").data(conFactoryService.getList(custAccId, selectVo));
    }

    @ApiOperation("获取修改品牌商家前的数据查询")
    @GetMapping("/getBeforeUpdateSelect/{id}")
    public Result getBeforeUpdateSelect(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long id) {
        return Result.success("获取修改前的品牌商家数据成功").data(PageUtil.row(factoryService.getBeforeUpdateSelect(id)));
    }

    @ApiOperation("根据品牌商家ID获取商家详情")
    @GetMapping("/getFactoryDetail/{id}")
    public Result getFactoryDetail(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long id) {
        return Result.success("获取商家详情成功").data(PageUtil.row(conFactoryService.getFactoryDetail(id)));
    }

    @ApiOperation("审核品牌商家")
    @PutMapping("/checkFactory/{factoryId}/{checkType}")
    public Result checkFactory(HttpServletRequest request, @PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId,
                               @PathVariable @ApiParam(value = "审核类型：1-审核通过，2-审核驳回", required = true, example = "1") Integer checkType) {
        Assert.isTrue(Pattern.matches("^(1|2)$", checkType.toString()), "审核类型参数不合法");
        factoryService.checkFactory(factoryId, checkType);
        return Result.success("审核商家成功");
    }

    @ApiOperation("保存商家调整余额")
    @PostMapping("/saveFactoryAdjustMoney/{factoryId}")
    public Result saveFactoryAdjustMoney(HttpServletRequest request, @PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId,
                                         @RequestBody @Valid FactoryBalanceAdjustVo adjustVo) {
        AccounLoginInfo loginInfo = TokenUtil.getAccountLoginInfo(request);
        accountBalanceAdjustService.save(loginInfo, factoryId, adjustVo);
        return Result.success("调整余额成功");
    }

    @ApiOperation("获取商家详情之账号管理")
    @PostMapping("/getFactoryEmpAccountList/{factoryId}")
    public Result getFactoryEmpAccountList(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId,
                                           @RequestBody @ApiParam(value = "关键词搜索：姓名/手机号") SelectVo selectVo) {
        return Result.success("获取品牌商家的人员账号列表成功").data(factoryEmpService.getFactoryEmpAccountList(factoryId, selectVo));
    }

    @ApiOperation("获取商家详情之结算价管理")
    @PostMapping("/getFactoryOrderModuleSettlementList/{factoryId}")
    public Result getFactoryOrderModuleSettlementList(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取结算价列表成功").data(conFactoryService.getFactoryOrderModuleSettlementList(factoryId, pageUtil));
    }

    @ApiOperation("获取商家详情之充值记录")
    @PostMapping("/getFactoryRechargeList/{factoryId}")
    public Result getFactoryRechargeList(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId, @RequestBody FactoryRechargeListSearchVo searchVo) {
        return Result.success("获取充值记录列表成功").data(factoryRechargeService.getFactoryRechargeList(factoryId, searchVo));
    }

    @ApiOperation("获取商家详情之工单列表")
    @PostMapping("/getFactoryDetailOrderList/{factoryId}")
    public Result getFactoryDetailOrderList(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取商家工单列表列表成功").data(conFactoryService.getFactoryDetailOrderList(factoryId, pageUtil));
    }

    @ApiOperation("获取商家详情之工单费用列表")
    @PostMapping("/getNetworkDetailOrderFeeList/{factoryId}")
    public Result getFactoryDetailOrderFeeList(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取商家详情工单费用列表成功").data(conFactoryService.getFactoryDetailOrderFeeList(factoryId, pageUtil));
    }

    @ApiOperation("获取商家详情之工单配件列表")
    @PostMapping("/getFactoryDetailOrderSpareList/{factoryId}")
    public Result getFactoryDetailOrderSpareList(@PathVariable @ApiParam(value = "品牌商家ID", required = true) Long factoryId, @RequestBody PageUtil pageUtil) {
        return Result.success("获取商家详情工单配件列表成功").data(conWorkSpareService.getFactoryDetailOrderSpareList(factoryId, pageUtil));
    }

    @ApiOperation("删除商家")
    @PostMapping("/delete")
    public Result delete(@RequestBody @ApiParam(value = "商家ID", required = true) List<Long> factoryIds) {
        conFactoryService.deleteBatchByIds(factoryIds);
        return Result.success("删除商家成功");
    }

    @ApiOperation("获取商家充值审核表")
    @PostMapping("/getFactoryRechargeCheckList")
    public Result getFactoryRechargeCheckList(HttpServletRequest request, @RequestBody FactoryRechargeCheckListSearchVo search) {
        Long custAccId = TokenUtil.getcustAccIdByToken(request);
        return Result.success("获取商家充值审核表成功").data(conFactoryService.getFactoryRechargeCheckList(custAccId, search));
    }

    @ApiOperation("审核商家充值")
    @PostMapping("/checkFactoryRecharge")
    public Result checkFactoryRecharge(@RequestBody @Valid CheckFactoryRechargeVo rechargeVo) {
        conFactoryService.checkFactoryRecharge(rechargeVo);
        return Result.success("审核商家充值成功");
    }




}
